package cn.codeforfun.handler;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @author Administrator
 * @date 2017/2/28
 */
@Component
public class Handler {
  @Async
  public Future<String> handler1() throws InterruptedException {
    long start = System.currentTimeMillis();
    System.out.println("handler1已开始");
    //睡眠一秒
    Thread.sleep(1000);
    System.out.println("handler1已结束");
    long end = System.currentTimeMillis();
    System.out.println("handler1耗时:" + (end - start) + "毫秒");
    return new AsyncResult<>("handler1完成");
  }

  @Async
  public Future<String> handler2() throws InterruptedException {
    long start = System.currentTimeMillis();
    System.out.println("handler2已开始");
    //睡眠一秒
    Thread.sleep(1000);
    System.out.println("handler2已结束");
    long end = System.currentTimeMillis();
    System.out.println("handler2耗时:" + (end - start) + "毫秒");
    return new AsyncResult<>("handler2完成");
  }

  @Async
  public Future<String> handler3() throws InterruptedException {
    long start = System.currentTimeMillis();
    System.out.println("handler3已开始");
    //睡眠一秒
    Thread.sleep(1000);
    System.out.println("handler3已结束");
    long end = System.currentTimeMillis();
    System.out.println("handler3耗时:" + (end - start) + "毫秒");
    return new AsyncResult<>("handler3完成");
  }

  @Async
  public void async1() throws InterruptedException {
    System.out.println("async1");
    Thread.sleep(2000);
    System.out.println("async1 ---end");
  }

  @Async
  public void async2() {
    System.out.println("async2");
  }

  @Async
  public void async3() {
    System.out.println("async3");
  }

  @Async
  public void async4() {
    System.out.println("async4");
  }

  @Async
  public void async5() {
    System.out.println("async5");
  }


}
